package com.wsc.springbootmultidevdemo.msg.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.wsc.springbootmultidevdemo.msg.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * confirm 异步发送
 */
public class Send1 {
    private static final String QUEUE_NAME = "async_confirm";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        //指定消息投递模式为确认模式
        channel.confirmSelect();

        String exchange = "confirm_exc";
        String routingKey = "confirm.save";

        channel.basicPublish(exchange, routingKey, null, "confirm test".getBytes());

        //添加一个确认监听
        channel.addConfirmListener(new ConfirmListener() {
            /**
             * 投递成功进入这个方法
             * @param l deliveryTag 消息的唯一标签
             * @param b multiple 是否是多条消息
             * @throws IOException
             */
            @Override
            public void handleAck(long l, boolean b) throws IOException {
                System.err.println("----- ack! -----");
            }

            /**
             * 消息发送失败进入这个方法
             * @param l deliveryTag 消息的唯一标签
             * @param b multiple 是否是多条消息
             * @throws IOException
             */
            @Override
            public void handleNack(long l, boolean b) throws IOException {
                System.err.println("----- nack! -----");
            }
        });


    }

}
